/*
	Alessandro Martinelli's Jogl Tutorial
    Copyright (C) 2008  Alessandro Martinelli  <alessandro.martinelli@unipv.it>

    This file is part of Alessandro Martinelli's Jogl Tutorial.

    Alessandro Martinelli's Jogl Tutorial is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Alessandro Martinelli's Jogl Tutorial is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Alessandro Martinelli's Jogl Tutorials.  If not, see <http://www.gnu.org/licenses/>.
 */


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import javax.swing.JFrame;

import com.sun.opengl.util.Animator;

/**
 * Introduction to light.
 * 
 * 
 * Keyboard commands: 
 * 
 * Key A) Increase the deltaZ value 
 * 
 * Key B) Decrease the deltaZ value 
 * 
 * This practice will introduce:
 * 
 * 1)       glu.gluQuadricNormals(quadric, GL.GL_TRUE);
 * 2)       gl.glEnable(GL.GL_LIGHTING);
 * 3)       glLight
 * 4)       glLightModel
 * 5)       glColorMaterial
 * 
 * @author Alessandro Martinelli
 */
public class Practice09IlluminateSphere extends JFrame implements KeyListener{
	
	private static float deltaZ=0;
	
	
	public static void main(String[] args) {
		Practice09IlluminateSphere frame=new Practice09IlluminateSphere();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public Practice09IlluminateSphere(){
		setSize(600,600);
		setTitle("Hello Universe");
		
		GraphicListener listener=new GraphicListener();
		GLCanvas canvas = new GLCanvas(new GLCapabilities());
	    canvas.addGLEventListener(listener);
	    getContentPane().add(canvas);
	    
	    //Creo un Animator e lo collego al Canvas
	    Animator animator = new Animator(canvas);
	    animator.start();
	    
	    addKeyListener(this);
	}
	
	public class GraphicListener implements GLEventListener{
		

		public void display(GLAutoDrawable arg0) {
			
			GL gl=arg0.getGL();
			gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);//variabile float che oscilla in funzione del time

			gl.glEnable(GL.GL_DEPTH_TEST);
			gl.glCullFace(GL.GL_FRONT); 
			gl.glEnable(GL.GL_CULL_FACE);
			gl.glFrontFace(GL.GL_CCW);

			GLU glu=new GLU();
			GLUquadric quadric=glu.gluNewQuadric();
			glu.gluQuadricNormals(quadric, GL.GL_TRUE);
			
			gl.glLoadIdentity();
			gl.glTranslatef(-0.5f,0,0);

				gl.glColor3f(0.7f,1,0.7f);
				glu.gluSphere(quadric, 0.5f, 90, 90);
			
			gl.glLoadIdentity();
			gl.glTranslatef(0.5f,0,deltaZ);
			
				gl.glColor3f(1,0.7f,0.7f);
				glu.gluSphere(quadric, 0.4f, 90, 90);
			
		}

		public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
			
		}

		public void init(GLAutoDrawable arg0) {

			GL gl=arg0.getGL();
			gl.glEnable(GL.GL_LIGHTING);

			float ambient[]= {0.2f,0.2f,0.2f,1};
			gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT , ambient,0);
			
			gl.glEnable(GL.GL_LIGHT0);
			float position[]= {-0.4f,0.5f,0.7f,1};
			gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);
			float intensity[]= {1,1,1,1};
			gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, intensity, 0);
			
			gl.glEnable(GL.GL_LIGHT1);
			float position2[]= {0,-0.8f,0.3f,1};
			gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, position2, 0);
			float intensity2[]= {1,0,0,0};
			gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, intensity2, 0);
			float specIntensity2[]= {1,1,1,1};
			gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, specIntensity2, 0);
			
			
			gl.glEnable(GL.GL_COLOR_MATERIAL);
			gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
			float specColor[]= {1,1,1,1};
			gl.glMaterialfv(GL.GL_FRONT_AND_BACK,GL.GL_SPECULAR, specColor,0);
			gl.glMaterialf(GL.GL_FRONT_AND_BACK,GL.GL_SHININESS, 80);
			
		}

		public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
			
		}
	}
	
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode()==KeyEvent.VK_A){
			deltaZ+=0.05;
		}
		if(arg0.getKeyCode()==KeyEvent.VK_B){
			deltaZ-=0.05;
		}
	}

	public void keyReleased(KeyEvent arg0) {
		
	}

	public void keyTyped(KeyEvent arg0) {
		
	}


	static final long serialVersionUID=100;
}